package HelloSeries.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import HelloSeries.conf.Configuration;
import HelloSeries.metier.NomTracker;
import HelloSeries.metier.Tracker;

public class SQLLiteDB {

	private Connection conn;
	private static Logger logger = Logger.getLogger(SQLLiteDB.class);

	public SQLLiteDB(String path) {
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:"+path);
		} catch (ClassNotFoundException e) {
			logger.error("[SQLLiteDB] "+e.getMessage());
		} catch (SQLException e) {
			logger.error("[SQLLiteDB] "+e.getMessage());
		}
	}
	public boolean searchCookie(NomTracker nomTracker, BrowserCookieTable browserCookieTable) {
		boolean hasResults = false;
		switch (nomTracker) {
			case ALLOTRACKER:  hasResults = allotrackerSearch(browserCookieTable);break;
            case FRENCHTORRENTDB:
                hasResults = frenchTorrentDBSearch( browserCookieTable );
                break;
		}
		if (hasResults){
			Configuration.getInstance().getTrackerList().updateXMLData();
		}
		return hasResults;
	}

    private boolean frenchTorrentDBSearch( BrowserCookieTable browserCookieTable )
    {
        try
        {
            Statement statement = conn.createStatement();
            String query =
                "SELECT * FROM " + browserCookieTable.getTableName() + " WHERE "
                    + browserCookieTable.getHostColumnName() + " LIKE '%frenchtorrentdb%' ";

            ResultSet results = statement.executeQuery( query );
            Tracker tracker = Configuration.getInstance().getTrackerList().getTracker( NomTracker.FRENCHTORRENTDB );
            while ( results.next() )
            {
                tracker.addCookieKey( results.getString( "name" ), results.getString( "value" ) );
            }
            statement.close();
            logger.info( "Liste des cookies : " + tracker.getDescription() );
            return verifyFrenchTorrentDBData( tracker );
        }
        catch ( SQLException e )
        {
            logger.error( "[SQLLiteDB] " + e.getMessage() );
            return false;
        }
    }

    
	private boolean allotrackerSearch( BrowserCookieTable browserCookieTable )
    {
		try {
			Statement statement = conn.createStatement();
			String query = "SELECT * FROM "+browserCookieTable.getTableName()+" WHERE "+browserCookieTable.getHostColumnName()+" LIKE '%allotracker%' ";

			ResultSet results = statement.executeQuery(query);
			Tracker tracker = Configuration.getInstance().getTrackerList().getTracker(NomTracker.ALLOTRACKER);
			while (results.next()) {
				tracker.addCookieKey(results.getString("name"),results.getString("value"));
			}
			statement.close();
            logger.info( "Liste des cookies : " + tracker.getDescription() );

            return verifyAllotrackerData( tracker );
		} catch (SQLException e) {
			logger.error("[SQLLiteDB] "+e.getMessage());
			return false;
		}
	}

	private boolean verifyAllotrackerData(Tracker tracker){
		List<String> keys = new ArrayList<String>();
		keys.add("AT_auth");
		keys.add("AT_key");
		return tracker.getDescription().keySet().containsAll(keys);
	}
	
	private boolean verifyFrenchTorrentDBData(Tracker tracker) {
		List<String> keys = new ArrayList<String>();
		keys.add("WebsiteID");
		return tracker.getDescription().keySet().containsAll(keys);
	}
}
